package com.ms.game.lobby.db;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

import org.hibernate.Query;
import org.hibernate.Session;

import com.ms.game.lobby.bulletin.BulletinModelInfo;
import com.xpec.c4.db.basic.BasicGameDAO;
import com.xpec.c4.db.hibernate.manager.HibernateManager;
import com.xpec.c4.game.time.TimeSys;
import com.xpec.c4.game.utility.GameUtility;

public class BulletinDAO extends BasicGameDAO{
	
	public static ArrayList<BulletinModel> getAllBulletinList(){
		Session session = HibernateManager.getSession();
		Query query = session.createQuery("from BulletinModel");
		ArrayList<BulletinModel> list = (ArrayList<BulletinModel>)query.list();
		HibernateManager.txCommitFailRollbackClearSession();
		return list;
	}
	
	public static ArrayList<BulletinModel> getBulletinList(){
		Session session = HibernateManager.txBegin();
		Date date = new Date(TimeSys.currentTimeMills());
		Query query = session.createQuery("from BulletinModel where startTime <= :startTime and endTime > :endTime and status = 1");
		query.setParameter("startTime", date);
		query.setParameter("endTime", date);
		ArrayList<BulletinModel> list = (ArrayList<BulletinModel>)query.list();
		
		HibernateManager.txCommitFailRollbackClearSession();
		
		return list;
	}
	
	public static HashMap<Integer, BulletinModelInfo> getBulletinInfoMap(){
		Session session = HibernateManager.txBegin();
		Date date = new Date(TimeSys.currentTimeMills());
		Query query = session.createQuery("from BulletinModel where startTime <= :startTime and endTime > :endTime and status = 1");
		query.setParameter("startTime", date);
		query.setParameter("endTime", date);
		ArrayList<BulletinModel> list = (ArrayList<BulletinModel>)query.list();
		HibernateManager.txCommitFailRollbackClearSession();
		
		HashMap<Integer, BulletinModelInfo> bulletinMap = new HashMap<Integer, BulletinModelInfo>();
		
		for(BulletinModel mo:list){
			BulletinModelInfo bulletinModelInfo = new BulletinModelInfo();
			bulletinModelInfo.setId(mo.getId());
			bulletinModelInfo.setTitle(mo.getTitle());
			bulletinModelInfo.setContent(mo.getContent());
			
			bulletinModelInfo.setCreateTime(GameUtility.transformTimeMilliToSec(mo.getCreateTime().getTime()));
			bulletinModelInfo.setEndTime(GameUtility.transformTimeMilliToSec(mo.getEndTime().getTime()));
			bulletinModelInfo.setModifyTime(GameUtility.transformTimeMilliToSec(mo.getModifyTime().getTime()));
			bulletinModelInfo.setStartTime(GameUtility.transformTimeMilliToSec(mo.getStartTime().getTime()));
			bulletinModelInfo.setStatus(mo.getStatus());
			bulletinModelInfo.setType(mo.getType());
			
			bulletinMap.put(bulletinModelInfo.getId(), bulletinModelInfo);
		}
		return bulletinMap;
	}
	
	public static BulletinModel get(int id){
		Session session = HibernateManager.txBegin();
		Query query = session.createQuery("from BulletinModel where id = :id");
		query.setParameter("id", id);
		BulletinModel model = (BulletinModel)query.uniqueResult();
		HibernateManager.txCommitFailRollbackClearSession();
		
		return model;
	}
	
	public static Integer add(BulletinModel model)
	{
		Integer id = null;
		Session session =  HibernateManager.txBegin();
		session.saveOrUpdate(model);
		HibernateManager.txCommitFailRollbackClearSession();
		id = model.getId();
		return id;
	}
	
	
	public static boolean update(BulletinModel model)
	{
		Session session =  HibernateManager.txBegin();
		session.saveOrUpdate(model);
		return HibernateManager.txCommitFailRollbackClearSession();
	}
}
